#===============================================================================
#  File:    0A4-AppendixC2-Lag-Structures.R
#  Date:    June, 2022 
#  Author:  Natalia Umansky
#  Purpose: Replicating figures displayed in the Appendix C.2
#===============================================================================

# LIBRARIES
#===============================================================================
library(data.table)
library(dplyr)
library(ggplot2)
library(ggforce)
library(cowplot)


# Import data
#===============================================================================

all_irf_data_wide <- fread("~/onetime-structural-shock-irfs-results_all.csv")

# Lag structures
#===============================================================================

lags <- function(d, grp){
  plot_db <- all_irf_data_wide %>%
    filter(day == d) %>%
    # - looking only at one time 10 point effects
    filter(data_type == 'Effect of a one time 10 percentage point attention increase at day 0') %>%
    filter(cov == grp) %>%
    mutate(data_type = paste("Effect of", grp, "on:")) %>%
    rename(x = cov, y = out)
  
  return(plot_db)
}

plotting <- function (x){
  result <- ggplot(x,
                   aes(x = y, y = pe, ymin = lwr, ymax = upr, group = day, color=day)) +
    #geom_segment(aes(x = y, xend = y, y = lwr, yend = upr, group = day), 
    #size = 4, alpha = 0.3, color= "blue") +
    geom_linerange(size = 2.5, position = position_dodge(width = 0.55), alpha = 0.3)+
    #geom_segment(alpha = 0.8, size = 0.5) +
    geom_point(position = position_dodge(width = 0.55), size = 5)+
    geom_hline(yintercept = 0, color = "red", size= 3) +
    #facet_grid(~day) +
    facet_wrap(~data_type) +
    coord_flip() +
    geom_vline(xintercept = 15) +
    xlab("") +
    ylab("")+
    #scale_y_continuous("\nThe effect of a one time 10 percentage point increase in attention in day 0",
    #expand = c(0,0.001)) +
    theme(
      panel.spacing = unit(0, units = "cm"),
      legend.position = "none", # change to none once second scale is added
      panel.background = element_blank(),
      panel.grid.major = element_line(colour = "gray90", linetype = "solid"),
      axis.text = element_text(size = 16),
      axis.text.y = element_text(hjust=0),
      strip.text = element_text(size = 16),
      panel.border = element_rect(colour = "black", fill = FALSE),
      strip.background = element_rect(colour = "black"),
      axis.title = element_text(size = 14),
      legend.text = element_text(size = 16)
    )
  return(result)
}

# The Media

lags_list= list()
for (i in 1:15){
  lags_list <- append(lags_list, list(lags(i, "Media")))
}

lags_media <- do.call("rbind", lags_list)

plot_media <- plotting(lags_media)

# Advocates

lags_list= list()
for (i in 1:15){
  lags_list <- append(lags_list, list(lags(i, "Advocates")))
}

lags_adv <- do.call("rbind", lags_list)

plot_adv <- plotting(lags_adv)

# Politicians

lags_list= list()
for (i in 1:15){
  lags_list <- append(lags_list, list(lags(i, "Politicians and\n Governmental Institutions")))
}

lags_pol <- do.call("rbind", lags_list)

plot_pol <- plotting(lags_pol)

# Citizens

lags_list= list()
for (i in 1:15){
  lags_list <- append(lags_list, list(lags(i, "Citizens on\n Twitter")))
}

lags_foll <- do.call("rbind", lags_list)

plot_foll <- plotting(lags_foll)

# Final

plot_all <- plot_grid(plot_foll+ theme(legend.position="none", panel.border = element_rect(colour = "gray", fill=NA, size=0.5)),
                      plot_pol+ theme(legend.position="none", panel.border = element_rect(colour = "gray", fill=NA, size=0.5)),
                      plot_adv+ theme(legend.position="none", panel.border = element_rect(colour = "gray", fill=NA, size=0.5)),
                      plot_media+ theme(legend.position="none", panel.border = element_rect(colour = "gray", fill=NA, size=0.5)),
                      nrow=2)

tnot <- ggplot(lags_foll,
               aes(x = y, y = pe, ymin = lwr, ymax = upr, group = day, color=day)) +
  #geom_segment(aes(x = y, xend = y, y = lwr, yend = upr, group = day), 
  #size = 4, alpha = 0.3, color= "blue") +
  geom_linerange(size = 2.5, position = position_dodge(width = 0.55), alpha = 0.3)+
  #geom_segment(alpha = 0.8, size = 0.5) +
  geom_point(position = position_dodge(width = 0.55), size = 5)+
  geom_hline(yintercept = 0, color = "red", size= 3) +
  #facet_grid(~day) +
  facet_wrap(~data_type) +
  coord_flip() +
  geom_vline(xintercept = 15) +
  xlab("") +
  #ylab("")+
  scale_y_continuous("\nThe effect of a one time 10 percentage point increase in attention in day 0",
                     expand = c(0,0.001)) +
  theme(
    panel.spacing = unit(0, units = "cm"),
    legend.position = "bottom", 
    panel.background = element_blank(),
    panel.grid.major = element_line(colour = "gray90", linetype = "solid"),
    axis.text = element_text(size = 16),
    axis.text.y = element_text(hjust=0),
    strip.text = element_text(size = 16),
    panel.border = element_rect(colour = "black", fill = FALSE),
    strip.background = element_rect(colour = "black"),
    axis.title = element_text(size = 14),
    legend.text = element_text(size = 16)
  )

legend <- get_legend(
  tnot+ theme(legend.box.margin = margin(0, 0, 0, 12),
              legend.position = c(0.4, 0.12),
              legend.text = element_text(size = 13),
              legend.title = element_text(size = 13))+
    guides(colour = guide_legend(override.aes = list(size=9))))

final <- plot_grid(plot_all, legend, rel_widths = c(2, .1), rel_heights = c(5, .2))
ggsave("Figure-A6.pdf", final, width=20, height=10)
